from game import Game, GamePosition, GameGUI
from game import AI
import os
import _thread
import numpy as np
from typing import List


def convert_map_data(height: int, width: int, data_map: List[str]) -> np.ndarray:
    data = []
    for i in range(height):
        row = []
        for j in range(width):
            row.append(data_map[i][j])
        data.append(row)
    return np.array(data)


map_data = convert_map_data(3, 5, [".....", "#..#.", "...$."])


def train_process(gui: GameGUI):
    game = Game(map_data, 5, gui)
    model_path = os.path.join(os.getcwd(), "models/vision.h5")
    ai = AI(10, game, model_path, 10, 0.7, 0.9)
    ai.train()
    pass


def gui_begin():
    gui = GameGUI(20, map_data)
    gui.after(100, train_process, gui)
    gui.mainloop()


def debug():
    print("debug........")


# def test_gui(callback):
#     import tkinter as tk
#     import numpy as np
#     print("test_gui")
#     UNIT = 40  # pixels
#     MAZE_H = 4  # grid height
#     MAZE_W = 4  # grid width
#     root = tk.Tk()
#     canvas = tk.Canvas(root, bg="white", height=MAZE_H * UNIT, width=MAZE_W * UNIT)
#     # create grids
#     for c in range(0, MAZE_W * UNIT, UNIT):
#         x0, y0, x1, y1 = c, 0, c, MAZE_H * UNIT
#         canvas.create_line(x0, y0, x1, y1)
#     for r in range(0, MAZE_H * UNIT, UNIT):
#         x0, y0, x1, y1 = 0, r, MAZE_W * UNIT, r
#         canvas.create_line(x0, y0, x1, y1)
#     # create origin
#     origin = np.array([20, 20])
#
#     # hell
#     hell1_center = origin + np.array([UNIT * 2, UNIT])
#     hell1 = canvas.create_rectangle(
#         hell1_center[0] - 15, hell1_center[1] - 15,
#         hell1_center[0] + 15, hell1_center[1] + 15,
#         fill='black')
#     # hell
#     # hell2_center = origin + np.array([UNIT, UNIT * 2])
#     # self.hell2 = self.canvas.create_rectangle(
#     #     hell2_center[0] - 15, hell2_center[1] - 15,
#     #     hell2_center[0] + 15, hell2_center[1] + 15,
#     #     fill='black')
#
#     # create oval
#     oval_center = origin + UNIT * 2
#     oval = canvas.create_oval(
#         oval_center[0] - 15, oval_center[1] - 15,
#         oval_center[0] + 15, oval_center[1] + 15,
#         fill='yellow')
#
#     # create red rect
#     rect = canvas.create_rectangle(
#         origin[0] - 15, origin[1] - 15,
#         origin[0] + 15, origin[1] + 15,
#         fill='red')
#
#     # pack all
#     canvas.pack()
#     root.after(100, callback)
#     root.mainloop()
#     pass


if __name__ == '__main__':
    # train_process()
    # test_gui(debug)
    # _thread.start_new_thread(test_gui, ())
    # print("loop after?")
    gui_begin()
